﻿/// <reference path="../_references.js" />
(function (e) {

    var configureIndicators = function (form) {
        var valOpts = $.data(form, 'validator').settings;
        var baseError = valOpts.errorPlacement;
        valOpts.errorPlacement = function (error, element) {
            baseError(error, element);
            if (error.text() != "") {
                var name = $(element).attr("name");
                $('span[data-valmsg-for="' + name + '"]').tooltip({
                    extraClass: "error-tooltip ui-corner-all",
                    top: -25,
                    left: 10,
                    bodyHandler: function () {
                        return error.text();
                    }
                });
            }
        };
        valOpts.unhighlight = function (element, errorClass, validClass) {
            baseunhighlight.call($.data(form, 'validator'), element, errorClass, validClass);
        };
    };

    e.init = function () {

        //validation
        e.validateOptions = function(options) {
            return $.extend({
                errorPlacement: function(error, element) {
                    var text = error.text();
                    e.log(text);
                    if (text) {
                        error.appendTo(element.parent("div"));
                        error.tooltip();
                        error.attr('data-original-title', text);
                    }
                },
                errorElement: "span",
                highlight: function(element, ev) {
                    var error = $(element).closest('.control-group').find("span.error");
                    var gr = $(element).closest('.control-group');
                    gr.removeClass('success').addClass('error');
                    error.attr('data-original-title', error.html());
                    error.removeClass('success');
                },
                success: function(label) {
                    label.closest('.control-group').removeClass('error').addClass('success');
                    label.addClass('success');
                    label.attr('data-original-title', "OK!");
                }
            }, options);
        };

        //diagnostics
        e.log = function (message) {
            if (typeof console !== 'undefined' && typeof console.log !== 'undefined') {
                console.log(message);
            } else {
                alert(message);
            }
        };

        //events
        var eventManager = new e.eventManager();

        e.publish = function (eventName, data) {
            eventManager.publish.call(eventManager, eventName, data);
        };

        e.subscribe = function (eventName, callback, context) {
            eventManager.subscribe.call(eventManager, eventName, callback, context);
        };

        //flashes
        e.flash = new e.flashMessages($("#flashMessages"));

        //views
        var viewEngine = new e.viewEngine($("#content"));
        var viewManager = new e.viewManager(function (state) {
            viewEngine.invokeView.call(viewEngine, state);
        });
        e.changeView = function (viewName, params) {
            viewManager.changeView(viewName, params);
        };

        $(window).hashchange();
    };
} (this.eg = this.eg || {}));